package com.mdd.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.mdd.common.entity.Card;
import com.mdd.common.entity.CardLevel;
import com.mdd.common.entity.RechargeOrder;
import com.mdd.common.mapper.RechargeOrderMapper;
import com.mdd.common.mapper.card.CardLevelMapper;
import com.mdd.common.mapper.card.CardMapper;
import com.mdd.common.service.ICardAppService;
import com.mdd.common.util.Constants;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import com.mdd.common.vo.card.UserCardDetailVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 功能描述: 会员卡业务
 *
 * @author lrc
 * @date 2025/4/2 23:04
 */
@Service
@Slf4j
public class CardAppServiceImpl implements ICardAppService {

    @Resource
    CardMapper cardMapper;

    @Resource
    CardLevelMapper cardLevelMapper;

    @Resource
    RechargeOrderMapper rechargeOrderMapper;

    @Override
    public UserCardDetailVo getCardDetail(Integer userId) {
        LambdaQueryWrapper<Card> wrapper = new LambdaQueryWrapper<Card>().eq(Card::getUserId, userId).last("limit 1");
        Card card = cardMapper.selectOne(wrapper);
        Assert.notNull(card, "会员卡不存在");
        Optional<CardLevel> level = Optional.ofNullable(cardLevelMapper.selectById(card.getCardLevelId()));
        UserCardDetailVo vo = new UserCardDetailVo();
        BeanUtils.copyProperties(card, vo);
        vo.setBackUrl(UrlUtils.toAbsoluteUrl(level.map(CardLevel::getBackUrl).orElse("")));
        vo.setColor(level.map(CardLevel::getColor).orElse(""));
        vo.setLevelName(level.map(CardLevel::getLevelName).orElse(""));
        return vo;
    }

    @Override
    public void bindCard(Integer userId, String mobile) {
        LambdaQueryWrapper<Card> wrapper = new LambdaQueryWrapper<Card>().eq(Card::getMobile, mobile).eq(Card::getIsDelete, Constants.STATUS_OK).last("limot 1");
        Card card = cardMapper.selectOne(wrapper);
        if (Objects.nonNull(card) && Objects.isNull(userId)) {
            card.setUpdateBy(Long.valueOf(userId));
            card.setUserId(userId);
            card.setUpdateTime(TimeUtils.timestamp());
            cardMapper.updateById(card);
            // 更新充值记录
            LambdaQueryWrapper<RechargeOrder> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(RechargeOrder::getOrderSn, card.getCardNo());
            queryWrapper.eq(RechargeOrder::getUserId, 0);
            List<RechargeOrder> orders = rechargeOrderMapper.selectList(queryWrapper);
            if (!CollectionUtils.isEmpty(orders)) {
                orders.forEach(item -> {
                    item.setUserId(userId);
                    rechargeOrderMapper.updateById(item);
                });
            }
        }
    }

}
